package com.yuanfeng.userms.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yuanfeng.commoms.config.YFConfig;
import com.yuanfeng.commoms.dto.userms.UserInfoDTO;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ParseTokenUtils;
import com.yuanfeng.commoms.util.RedisUtil;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import com.yuanfeng.userms.entity.UserInfoDetaileEntity;
import com.yuanfeng.userms.mapper.UserInfoDetailMapper;
import com.yuanfeng.userms.service.UserInfoDetailService;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class UserInfoDetailServiceImpl extends ServiceImpl<UserInfoDetailMapper, UserInfoDetaileEntity>
    implements UserInfoDetailService{

    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public ResponseResult userGroupingStatistics(Map<String, Object> map) {
        String token = (String) map.get("token");
        boolean isExistToken = RedisUtil.exists(YFConfig.YF_PLATFORM_TOKEN_USER + token);
        if (isExistToken) {
            try {
                List<Map<String,Object>> goodsGroupingList = this.baseMapper.queryUserGrouping();
                if (!goodsGroupingList.isEmpty()) {
                   return ResponseResult.success(goodsGroupingList);
                } else {
                    return ResponseResult.fail(BizCodeEnume.NO_INFORMATION_OBTAINED);
                }
            } catch (Exception e) {
                return ResponseResult.fail( e.getMessage());
            }
        } else {
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_INVALID);
        }
    }

    @Override
    public void updateUserAtarUrl(Map<String, Object> updateUserParam) {
        this.baseMapper.updateUserAtarUrl(updateUserParam);
    }

    @Override
    public ResponseResult getUserSecurityLevel(Map<String, String> paraMap) {
        UserInfoVO userInfo = ParseTokenUtils.getUserInfoByToken(paraMap.get("token"));
        if (null == userInfo){
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_INVALID.getCode(),BizCodeEnume.TOKEN_IS_INVALID.getMsg());
        }
        Map<String, Object> map = new HashedMap();
        // 查询用户是否实名认证
        Integer shiming = this.baseMapper.queryShiming(userInfo.getUserId());
        if (0 != shiming) {
            map.put("shiMing", true);
        } else {
            map.put("shiMing", false);
        }
        //查询用户是否设置登录密码
        Integer loginPassword = this.baseMapper.queryLoginPassword(userInfo.getUserId());
        if(0 != loginPassword){
            map.put("loginPassword",true);
        }else{
            map.put("loginPassword",false);
        }
        //查询用户是否设置支付密码
        Integer payPassword = this.baseMapper.queryPayPassword(userInfo.getUserId());
        if (0 != payPassword) {
            map.put("payPassword", true);
        } else {
            map.put("payPassword", false);
        }
        return ResponseResult.success(map);
    }

    @Override
    public ResponseResult delUserInfo(String userId) {
        // 删除用户信息
        this.baseMapper.deleteUserInfo(userId);
        this.baseMapper.deleteUserDetail(userId);
        this.baseMapper.deleteUserConnect(userId);
        //删除微客信息
        this.baseMapper.deleteVipUser(userId);
        this.baseMapper.deleteVkApply(userId);
        this.baseMapper.deleteVkUser(userId);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult updateUserNickname(Map<String, String> paraMap) {
        String token = paraMap.get("token");
        String json = RedisUtil.get(YFConfig.YF_USER_TOKEN_USER + token);
        if (null == json || "".equals(json)) {
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_INVALID);
        }
        UserInfoVO userInfo = ParseTokenUtils.getUserInfoByTokenNoIsExist(token);
        paraMap.put("userId", userInfo.getUserId());
        Integer torf = this.updateGender(paraMap);
        if (1 == torf) {
            String mobilePhone = userInfo.getUserMobile();
            UserInfoVO newUserInfo = this.baseMapper.getUserInfoByPhoneNumber(mobilePhone);
            String bindOpenid = userInfo.getBindOpenid();
            if (StringUtils.isNotEmpty(bindOpenid)) {
                //根据openid获取用户昵称
                Map<String, String> outMap = this.baseMapper.selectInfo(bindOpenid);
                if (StringUtils.isNotEmpty(outMap.get("bind_avator"))) {
                    newUserInfo.setUserAvatar(outMap.get("bind_avator"));
                }
            }
            Integer status = this.baseMapper.queryShopStatus(userInfo.getUserId());
            if (status > 0) {
                newUserInfo.setIsShop(1);
            } else {
                newUserInfo.setIsShop(2);
            }
            try {
                String ui = RedisUtil.get(YFConfig.YF_USER_TOKEN_USER + token);
                UserInfoDTO dto = JSON.parseObject(ui, UserInfoDTO.class);
                dto.setUser(newUserInfo);
                RedisUtil.set(YFConfig.YF_USER_TOKEN_USER + token, JSON.toJSONString(dto),
                        YFConfig.TOKEN_OUTTIME);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return ResponseResult.success(1, "更新成功");
        }
        return ResponseResult.fail(2, "更新失败");
    }

    public Integer updateGender(Map<String, String> paraMap) {
        Integer torf = 1;
        if (paraMap.get("userGender") != null) {
            torf = this.baseMapper.updateGender(paraMap);

        }
        if (paraMap.get("userNickname") != null) {
            torf = this.baseMapper.updateUserName(paraMap);
            this.baseMapper.updateUserBindConnect2(paraMap);
        }
        return torf;
    }
}




